/* 
 * Copyright (C) 2012 UTSChedule
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package utsc.apps.utschedule.widget;

import utsc.apps.utschedule.R;
import utsc.apps.utschedule.util.AppUtils;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.util.AttributeSet;
import android.widget.RelativeLayout;
import android.widget.TextView;

/**
 * This is a custom layout widget for setting the background of a form section/group.
 * @author Wenbin
 *
 */
public class FormLayout extends RelativeLayout {

	private int _color=Color.WHITE;
	private TextView _frmHeader;
	private int _headerColor;
	
	public FormLayout(Context context){
		this(context, null);
	}
	
	public FormLayout(Context context, AttributeSet attrs){
		super(context, attrs);
		 setupLayout( context, attrs, 0);
	}
	
	public FormLayout(Context context, AttributeSet attrs, int defStyle) {
		
		super(context, attrs, defStyle);
		 setupLayout( context, attrs, defStyle);
	}
	
	private void setupLayout(Context context,AttributeSet attrs,int defStyle) {
		 final TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.FormLayout);
		 
		 _headerColor = a.getResourceId(R.styleable.FormLayout_formHeaderColor, R.color.utsc_purple);
	        a.recycle();
		
		setBackgroundDrawable(AppUtils.getColorCustomizableDrawable(context, R.drawable.form_border, _color, -1, 0).get());
	}
	
	@Override
	protected void onLayout(boolean changed, int l, int t, int r, int b) {
		if (_frmHeader==null) {
			_frmHeader=(TextView) findViewById(R.id.frm_txt_header);
			if (_frmHeader!=null) {
				_frmHeader.setBackgroundDrawable(AppUtils.getColorCustomizableDrawable(getContext(), R.drawable.form_header, getResources().getColor(_headerColor), -1, 0).get());
			}
		}
		
		super.onLayout(changed, l, t, r, b);
	}

}
